package cn.codexin.wechatofficial.service;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author ly
 * @date 2020/8/20 0020 15:04
 */
@Service
public class TimeService {
    public String currentTime(String table,long endtime){
//        Calendar cal = Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
//        int month = cal.get(Calendar.MONTH) + 1;
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
        int year = Integer.parseInt(sdf1.format(endtime));
        int month = Integer.parseInt(sdf2.format(endtime));
        String currentTime = null;
        if(month<10){
            currentTime = table + year + "" + 0 + "" + month;
        }else {
            currentTime = table + year + "" + month;
        }
        return currentTime;
    }
    public String lastTime(String table,long endtime){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
        int year = Integer.parseInt(sdf1.format(endtime));
        int month = Integer.parseInt(sdf2.format(endtime));
        String lastTime = null;
        if (month==1){
            month = 12;
            year = year - 1;
            lastTime = table + year + "" + month;
        }else{
            month =  month -1;
            if (month<10){
                lastTime = table + year + "" + 0 + "" + month;
            }else {
                lastTime = table + year + "" + month;
            }
        }
        return lastTime;
    }
}
